#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* phead = new ListNode;
        ListNode* cur1 = l1, * cur2 = l2;
        int diff = 0;
        ListNode* pcur = phead;
        while (cur1 || cur2 || diff)
        {
            if (cur1)
            {
                diff += cur1->val;
                cur1 = cur1->next;
            }
            if (cur2)
            {
                diff += cur2->val;
                cur2 = cur2->next;
            }
            ListNode* tmp = new ListNode(diff % 10);
            pcur->next = tmp;
            pcur = tmp;
            diff /= 10;
        }
        pcur = phead->next;
        delete phead;
        return pcur;
    }
};